In [ ]:
# Standard csv python libraries
import csv
# Main python library for mathematical calculations
import numpy as np
from scipy.stats.stats import pearsonr
import scipy
# Plotting related python libraries
import matplotlib.pyplot as plt
# Open csv CO2 and weather files
userfile_CO2 = input("CO2 File: ")
userfile_Humid =input("Humidity File: ")
results_CO2 = csv.reader(open(userfile_CO2), delimiter=',')
results_Humid = csv.reader(open(userfile_Humid), delimiter=',')
# Append CO2 and humidity data into separate lists
CO2 = []
Humidity = []
row_counter1 = 0
row_counter2 = 0
for r in results_CO2:
row_counter1 += 1
if row_counter1>1:
CO2.append(int(r[1]))
for r in results_Humid:
row_counter2 += 1
if row_counter2>1:
Humidity.append(float(r[3]))
# Create n_merge and calculate nsum_data
n_merge = int(input("n data points to combine:"))
ndata = len(CO2)
nsum_data = int(ndata/n_merge)
# Append merged CO2 and humidity data into separate lists
CO2_ave = []
CO2_unc = []
Humid_ave = []
Humid_unc = []
for i in range(nsum_data):
idata1 = CO2[i*n_merge:(i+1)*n_merge]
idata_array1 = np.asarray(idata1)
CO2mean = np.mean(idata_array1)
CO2sigma = np.sqrt(np.var(idata_array1))
CO2_ave.append(CO2mean)
CO2_unc.append(CO2sigma)
idata2 = Humidity[i*n_merge:(i+1)*n_merge]
idata_array2 = np.asarray(idata2)
Humidity_mean = np.mean(idata_array2)
Humidity_sigma = np.sqrt(np.var(idata_array2))
Humid_ave.append(Humidity_mean)
Humid_unc.append(Humidity_sigma)
# Caculate correlation values
a = pearsonr(CO2_ave, Humid_ave)
b = scipy.stats.spearmanr(CO2_ave, Humid_ave)
print("Pearson r =", a[0])
print("P value =", a[1])
print("Spearman r =", b[0])
print("Spearman r=", b[1])
# Plot graph
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(CO2_ave, Humid_ave, "b.")
plt.title("Humidity vs CO2")
plt.xlabel("CO2 (ppm)")
plt.ylabel("Humidity (%)")
plt.legend()
# Show correlation values on graph
plt.text(0.6, 0.95, '%s %s' % ("Pearson r =",a[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.85, '%s %s' % ("P value =",a[1]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.75, '%s %s' % ("Spearman r =",b[0]), ha='center', va='center', transform = ax.transAxes)
plt.text(0.6, 0.65, '%s %s' % ("P value =",b[1]), ha='center', va='center', transform = ax.transAxes)
# Show graph
plt.show()